﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;
using System.ServiceModel;
namespace Infrastructure.Services.Interceptors
{
    public class WebTraceInterceptor : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            StringBuilder sb = new StringBuilder();              
            if ( ServiceSecurityContext.Current != null && ServiceSecurityContext.Current.PrimaryIdentity != null)
            {
                sb.AppendLine("Primary Authentication type " + ServiceSecurityContext.Current.PrimaryIdentity.AuthenticationType);
                sb.AppendLine("Primary Is Authenticate" + ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated);
                sb.AppendLine("Primary identity " + ServiceSecurityContext.Current.PrimaryIdentity.Name);
            }
            if (ServiceSecurityContext.Current != null &&  ServiceSecurityContext.Current.WindowsIdentity != null)
            {
                sb.AppendLine("Windows Authentication type " + ServiceSecurityContext.Current.WindowsIdentity.AuthenticationType);
                sb.AppendLine("Windows Is Authenticate" + ServiceSecurityContext.Current.WindowsIdentity.IsAuthenticated);
                sb.AppendLine("Windows identity " + ServiceSecurityContext.Current.WindowsIdentity.Name);
            }
            if (System.Web.HttpContext.Current != null)
                System.Web.HttpContext.Current.Trace.Warn(sb.ToString());
            return getNext()(input, getNext);
        }

        public int Order  {get;set;}        
    }
}
